package com.link.base.base.allowurl.controller;

import javax.annotation.Resource;

import com.link.base.base.user.model.User;
import com.link.core.basic.model.BasicModel;
import com.link.core.util.StringUtils;
import com.link.base.base.allowurl.model.AllowUrl;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.link.base.base.allowurl.service.AllowUrlService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 不拦截URL-不拦截URL
 *
 * @author 郭闪
 * @date 2019/07/23 10:13
 */
@Controller
@RequestMapping("/link/allowUrl")
public class AllowUrlController extends BasicController<AllowUrl> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private AllowUrlService allowUrlService;

    @Resource
    private RestTemplate restNoLbTemplate;

    @Resource
    private DiscoveryClient discoveryClient;

    @Resource
    private Environment env;

    @Override
    public BasicService<AllowUrl> getBasicService() throws Exception {
        return allowUrlService;
    }

    /**
     * 向各个gateway实例发起更新tokenFilter urlSet 请求
     *
     * @author  guoshan
     */
    @RequestMapping("updateUrlSet")
    public Map<String, Object> updateAllowUrls() {
//        User user = new BasicModel();
        BasicModel basicModel = new User();
        Map<String, Object> result = new HashMap<>(6);
        try {
            //获取服务列表，向每个服务发送更新url的请求
            String gatewayServiceName = env.getProperty("gateway.servicename");
            if (StringUtils.isBlank(gatewayServiceName)) {
                gatewayServiceName = "link-gateway";
            } else {
                gatewayServiceName = gatewayServiceName.toLowerCase();
            }

            List<ServiceInstance> instances = discoveryClient.getInstances(gatewayServiceName);

            for (ServiceInstance instance : instances) {

                String homeUrl = ((EurekaDiscoveryClient.EurekaServiceInstance) instance).getInstanceInfo().getHomePageUrl();
                String path = homeUrl + "gateway/allowUrl/updateProperties";
                Map<String, Boolean> response = this.restNoLbTemplate.postForObject(path, null, Map.class);
                if (!response.get("success")) {
                    result.put("success", false);
                    result.put("result", response.get("result"));
                    return result;
                }
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
            return result;
        }
        result.put("success", true);
        return result;
    }
}
